package com.syj.collect.http;


import android.content.Intent;


import com.syj.collect.app.MyApplication;
import com.syj.collect.model.LoginActivity;
import com.syj.collect.utils.LogUtils;
import com.syj.collect.utils.ToastMgr;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

public abstract class BaseObserver<T> implements Observer<T> {

    @Override
    public void onSubscribe(Disposable d) {

    }
    JSONObject jsonObject;
    @Override
    public void onNext(T value) {
        try {
                jsonObject=new JSONObject((String) value);
                int code = jsonObject.getInt("code");
//                LogUtils.i("onError="+code);
                switch (code){
                    case  401:
                        Intent intent=new Intent(MyApplication.context, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                        MyApplication.context.startActivity(intent);
                        return;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        onSuccess(value);
    }
    @Override
    public void onError(Throwable e) {
        try {
//            LogUtils.i("onError="+e);
            ToastMgr.builder.display("请检查您的网络状态");
            onFailure(e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    @Override
    public void onComplete() {

        onEnd();
    }
    //请求成功且返回码为200的回调方法,这里抽象方法申明
    public abstract boolean onSuccess(T value);

    //请求失败回调方法,这里抽象方法申明
    public abstract void onFailure(Throwable e) throws Exception;

    public abstract void onEnd();

}
